Global st1#,st2#,st3#,st4#,unbit$,fasecnt,maxfase1,minfase2,bit$,ty$,tyold$,tycnt,in$,in1,in2,in3,in4,in31,in41,in5,in6,in51,in61,in7,in8,in71,in81,in9,in10,in91,in101,in11,in12,in111,in121,in13,in14,untest,sort1,sort2,zzz1,hzzz1,jzzz1,yyyyy$,out$
in8 = CreateBank(200):in81 = CreateBank(200):in10 = CreateBank(200):in101 = CreateBank(200):in9 = CreateBank(200):in91 = CreateBank(200):in5 = CreateBank(200):in51 = CreateBank(200):in6 = CreateBank(200):in61 = CreateBank(200):in7 = CreateBank(200):in71 = CreateBank(200):in1 = CreateBank(200):in2 = CreateBank(200):in3 = CreateBank(200):in4 = CreateBank(200):in31 = CreateBank(200):in41 = CreateBank(200)
Dim decode(10000)
Dim encode(10000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(60):sort2 = CreateBank(60)
Dim zzz(66666):Dim zzz2(8881,100):Dim hzzz2(8881,100):Dim hzzz(66666):Dim jzzz2(8881,100):Dim jzzz(66666)
decodet():minfase2=800

Function getnums()
	For y=0 To 2
	For x=0 To 2
		Color 0,255,0:Text 30+x*30,y*9+10,PeekInt(in1,5*(y*3+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10)):Text 150,10,xx1,False ,False 
	xx2=fact(PeekInt(in1,0+15),PeekInt(in1,5+15),PeekInt(in1,10+15)):Text 150,19,xx2,False ,False 
	xx3=fact(PeekInt(in1,0+2*15),PeekInt(in1,5+2*15),PeekInt(in1,10+2*15)):Text 150,28,xx3,False ,False 
	lxx1 = fact(xx1,xx2,xx3)
	Text 160,22,lxx1,False ,False 
	
	xx1=fact(PeekInt(in1,0),PeekInt(in1,0+15),PeekInt(in1,0+2*15)):Text 180,10,xx1,False ,False 
	xx2=fact(PeekInt(in1,5),PeekInt(in1,5+15),PeekInt(in1,5+2*15)):Text 180,19,xx2,False ,False 
	xx3=fact(PeekInt(in1,10),PeekInt(in1,10+15),PeekInt(in1,10+2*15)):Text 180,28,xx3,False ,False 
	lxx2=fact(xx1,xx2,xx3)
	Text 190,22,lxx2,False ,False:Text 210,22,lxx1*6+lxx2,False ,False 

	fact_faza()
	erase()
	
	For y=0 To 2
	For x=0 To 2
	Color 255,255,0:Text 30+x*30,y*9+50,PeekInt(in2,5*(y*3+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
	xx2=fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
	xx3=fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
	lxx1 = fact(xx1,xx2,xx3)
	Text 160,62,lxx1,False ,False 

	xx1=fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15))
	xx2=fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15))
	xx3=fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15))
	lxx2 = fact(xx1,xx2,xx3)
	Text 190,62,lxx2,False ,False :Text 210,62,lxx1*6+lxx2,False ,False 	
	
;         STAT
;	For i=0 To hzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+hzzz(i)
;		Color 155,255,0:Text 15+(i*19) Mod 960,180+68*Int(i/80),Str(hzzz(i))+",",False ,False 
;			For i9=0 To hzzz2(i,100)-1
;			Color hzzz2(i,i9)*21,hzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,180+68*Int(i/80)+i9*10+10,Str(hzzz2(i,i9))+"",False ,False 	
;			Next	
;		If hzzz2(i,100) = 0 Then 
;		Color 155,hzzz2(i,99)*51,hzzz2(i,99)*51:Text 15+(i*19) Mod 960,180+68*Int(i/80)-7,Str(hzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
;
;	For i=0 To jzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+jzzz(i)
;		Color 105,255,0:Text 15+(i*19) Mod 960,255+68*Int(i/80),Str(jzzz(i))+",",False ,False 
;			For i9=0 To jzzz2(i,100)-1
;			Color jzzz2(i,i9)*21,jzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,255+68*Int(i/80)+i9*10+10,Str(jzzz2(i,i9))+"",False ,False 	
;			Next	
;		If jzzz2(i,100) = 0 Then 
;		Color 105,jzzz2(i,99)*51,jzzz2(i,99)*51:Text 15+(i*19) Mod 960,255+68*Int(i/80)-7,Str(jzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
	
	ttt0=0:wnullmax=0
	For i=0 To zzz1
		If zzz(i) = 1 Then 
			cnull = cnull +1
			If wnullmax < wnullmax0 Then wnullmax = wnullmax0
			wnullmax0=0
		EndIf
		If zzz(i) <> 0 Then wnullmax0=wnullmax0+1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+zzz(i)
		Color 255,255,0:Text 15+(i*19) Mod 960,110+68*Int(i/80),Str(zzz(i))+",",False ,False 
			For i9=0 To zzz2(i,100)-1
			Color zzz2(i,i9)*51,zzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,110+68*Int(i/80)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
			Next	
		If zzz2(i,100) = 0 Then 
		Color 255,zzz2(i,99)*51,zzz2(i,99)*51:Text 15+(i*19) Mod 960,110+68*Int(i/80)-7,Str(zzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	cnull = Ceil(cnull * 100 /  (zzz1+1))
	tnull = Ceil(tycnt* 100 /  (zzz1+1))

	Color 255,0,0:
	If bit=2 Text 140,89,"-",False ,False Else Color 0,255,0:Text 70,89,"+"

	Color 255,255,210:Text 10,95,tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", One cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False

End Function

Function fact(x1,x2,x3)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1
	For x=3 To 1 Step -1
	maxx =0
	For xt=0 To 2
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5))
	xx = encode(ttt*5)
	Return xx
End Function

Function mod4(x)
If x =2 Then x =1: Else x=2
Return x
End Function

Function mod3(x)
While x>7
 x = x -8
Wend 
While  x<0 
 x = x +8
Wend 
Return x
End Function

Function mod2(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function mod1(x)
While x>5 
 x = x -6
Wend 
While  x<0 
 x = x +6
Wend 
Return x
End Function

Function randnum(bt)
;For i = 0 To 9 
;t = Rand(0,65535):PokeInt in1,i*5, t:PokeInt in2,i*5, t:SeedRnd MilliSecs():t = Rand(0,65535):PokeInt in3,i*5, t:PokeInt in31,i*5, t:SeedRnd MilliSecs():t = Rand(0,65535):PokeInt in4,i*5, t:PokeInt in41,i*5, t
;Next

For i = 0 To 9 
t = Rand(0,7):PokeInt in1,i*5, t:PokeInt in2,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in3,i*5, t:PokeInt in31,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in4,i*5, t:PokeInt in41,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in5,i*5, t:PokeInt in51,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in6,i*5, t:PokeInt in61,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in7,i*5, t:PokeInt in71,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in8,i*5, t:PokeInt in81,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in9,i*5, t:PokeInt in91,i*5, t
Next
For i = 0 To 9 
t = Rand(0,65535):PokeInt in10,i*5, t:PokeInt in101,i*5, t
Next

If bt=1 Then
For i=0 To 66666
zzz1=0:jzzz1=0:hzzz1=0:tycnt=0
hzzz(i)=0:zzz(i)=0:jzzz(i)=0
Next
For i1=0 To 8881
For i2=0 To 100
jzzz2(i1,i2)=0
zzz2(i1,i2)=0
hzzz2(i1,i2)=0
Next
Next
EndIf

End Function


Function checke()

For i = 0 To 9 
If (PeekInt (in5,i*5) <> PeekInt (in31,i*5)) Then yyyyy$=String( "error",1):Text 410,i*9," "+ (PeekInt (in5,i*5)+" "+PeekInt (in31,i*5)),False ,False
If (PeekInt (in6,i*5) <> PeekInt (in1,i*5)) Then yyyyy$=String( "error",1):Text 410,90+i*9," "+ (PeekInt (in6,i*5)+" "+PeekInt (in1,i*5)),False ,False
If (PeekInt (in7,i*5) <> PeekInt (in41,i*5)) Then yyyyy$=String( "error",1):Text 410,180+i*9," "+ (PeekInt (in7,i*5)+" "+PeekInt (in41,i*5)),False ,False
Next
Color 255,0,0:Text 1,1,yyyyy,False ,False  
End Function


Function decodet()
y=0
For x1=1 To 3
For x2=1 To 3
For x3=1 To 3
If((x1 <> x2) And (x1<>x3) And (x2<>x3)) decode(y*5) =x1+""+x2+""+x3:encode(Int(x1+""+x2+""+x3)*5)=y:y=y+1
Next
Next
Next
End Function